package com.fofia.iotweb.framework.auth;

import com.fofia.iotweb.web.dto.JsonResult;
import org.noear.solon.annotation.Component;
import org.noear.solon.auth.AuthException;
import org.noear.solon.auth.AuthStatus;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;
import org.noear.solon.core.handle.Result;

//或者：使用过滤器，替代“默认的验证失败处理”（推荐）
@Component
public class SolonActionFilter implements Filter {

    @Override
    public void doFilter(Context ctx, FilterChain chain) throws Throwable {
        try {
            chain.doFilter(ctx);
        } catch (AuthException e) {
            AuthStatus status = e.getStatus();
            //ctx.render(Result.failure(status.code, status.message));
            ctx.render(JsonResult.forbidden());
        }
    }
}
